﻿using System;
using Glue;
using Glue.Internals.Verification;
using Xunit;

namespace GlueTests.MapperVerificationTests
{
    public class When_mapping_towards_field
    {
        public class DomainPerson { public int Id { get; set; } }
        public class GuiPerson { public String Id; }

        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private readonly MapperVerification<DomainPerson, GuiPerson> verification;

        public When_mapping_towards_field()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            verification = new MapperVerification<DomainPerson, GuiPerson>(mapping);
        }

        [Fact]
        public void Should_normally_succeed()
        {
            mapping.AddConverter(Converting.BetweenIntAndString());
            mapping.Relate(domain => domain.Id, gui => gui.Id);
            verification.AssertMapsCorrectlyTowards(new GuiPerson());
        }

        [Fact]
        public void If_converter_fails_Should_throw_exception()
        {
            mapping.AddConverter(new ReturnSameConverter("1", 1));
            mapping.Relate(domain => domain.Id,gui=>gui.Id);
            Assert.Throws(typeof(GlueException),
                          () => verification.AssertMapsCorrectlyTowards(new GuiPerson { Id = "1" }));
        }
    }
}
